eststo clear


if("`c(os)'"=="Windows"){
	local tableSaveDir = "Tables\"
	local plotSaveDir = "Plots\"
	local pythonSaveDir = "PythonScripts\"
}
else{
	local tableSaveDir = "Tables/"
	local plotSaveDir = "Plots/"
	local pythonSaveDir = "PythonScripts/"
}

//Establish Control Variable Sets
//Spec1 controls are the same for both registrations and VMT 
local Spec1Controls = "c.normYear#i.stateGroup c.quadYear#i.stateGroup asinhMetroPop asinhNonMetroPop asinhMetroInc asinhNonMetroInc logrealmeangasprice logemployment loglicenseddrivers logrealstategdp logroadmileage"

//Other controls are not the same for both registrations and VMT 
//Registration Controls
local Spec2RegControls = "`Spec1Controls' i.transactionDataDummy"
local Spec3RegControls = "`Spec2RegControls' i.regDummyExtSpec3"
local Spec4RegControls = "`Spec2RegControls' i.regDummyExtSpec4"


//VMT Controls
local Spec2VMTControls = "`Spec1Controls'"
local Spec3VMTControls = "`Spec2VMTControls' i.vmtDummyExtSpec3"
local Spec4VMTControls = "`Spec2VMTControls' i.vmtDummyExtSpec4"

//Absorb Vars are always time and state fixed effects
local absorbVars = "ib51.stateGroup ib1995.year"

//Establish Cluster Variable
local clusterVar = "stateGroup year"

//Establish primary null
local primaryNull = 1.6
local altNull = 1
local altNull2 = -2.5
local altNull3 = 0.3
local altNull4 = 0

//Define treatment Variable
local treatmentVar = "1.nosafetyind"

//Set up program arguments for the Spec2 model
local fsVarsSpec2 = "logregistrations i.nosafetyind `Spec2RegControls'"
local rfVarsSpec2 = "logvmt i.nosafetyind `Spec2VMTControls'"

local fsOut = "logregistrations"
local rfOut = "logvmt"

local fsControls = "`Spec2RegControls'"
local rfControls = "`Spec2VMTControls'"

local treatControl = "nosafetyind"

local treatmentVar = "1.nosafetyind"

local modelTitleSpec2 = "Spec2"
local nullLowSpec2 = -4
local nullDeltaSpec2 = 0.001
local nullHighSpec2 = 1

levelsof statecode
	local index = 2
	qui putexcel set "`tableSaveDir'iv-loo.xlsx", sheet("LOO-Spec2VMT", replace) modify
	foreach tempSC in `r(levels)'{
		preserve
		
		qui putexcel A1 = "State Removed"
		qui putexcel B1 = "Pi"
		qui putexcel C1 = "Pi Variance"
		qui putexcel D1 = "Delta"
		qui putexcel E1 = "Delta Variance"
		qui putexcel F1 = "Pi-Delta Covariance"
		qui putexcel G1 = "AR(Beta=`primaryNull')"
		qui putexcel H1 = "P-Value(Beta=`primaryNull')"
		qui putexcel I1 = "AR(Beta=`altNull')"
		qui putexcel J1 = "P-Value(Beta=`altNull')"
		qui putexcel K1 = "AR(Beta=`altNull2')"
		qui putexcel L1 = "P-Value(Beta=`altNull2')"
		qui putexcel M1 = "AR(Beta=`altNull3')"
		qui putexcel N1 = "P-Value(Beta=`altNull3')"
		qui putexcel O1 = "AR(Beta=`altNull4')"
		qui putexcel P1 = "P-Value(Beta=`altNull4')"
		qui putexcel Q1 = "Beta"


		qui drop if statecode == "`tempSC'"
		jointEstimateFWL `fsOut', `rfOut', `fsControls', `rfControls', `treatControl', `absorbVars', `clusterVar', `treatmentVar', "`Spec2' w/o `tempSC'"
		local tempPi = `r(pi)'
		local tempDelta = `r(delta)'
		local tempPiVar = `r(piVar)'
		local tempDeltaVar = `r(deltaVar)'
		local tempCovar = `r(covar)'
		testNull `tempPi' `tempDelta' `tempPiVar' `tempDeltaVar' `tempCovar' `primaryNull'
		local tempPrimaryAR = `r(arStat)'
		local tempPrimaryPVal = `r(pVal)'
		local tempBeta = `r(beta)'
		testNull `tempPi' `tempDelta' `tempPiVar' `tempDeltaVar' `tempCovar' `altNull'
		local tempAltAR = `r(arStat)'
		local tempAltPVal = `r(pVal)'
		testNull `tempPi' `tempDelta' `tempPiVar' `tempDeltaVar' `tempCovar' `altNull2'
		local tempAltAR2 = `r(arStat)'
		local tempAltPVal2 = `r(pVal)'
		testNull `tempPi' `tempDelta' `tempPiVar' `tempDeltaVar' `tempCovar' `altNull3'
		local tempAltAR3 = `r(arStat)'
		local tempAltPVal3 = `r(pVal)'
		testNull `tempPi' `tempDelta' `tempPiVar' `tempDeltaVar' `tempCovar' `altNull4'
		local tempAltAR4 = `r(arStat)'
		local tempAltPVal4 = `r(pVal)'
		
		putexcel A`index' = "`tempSC'"
		qui putexcel B`index' = `tempPi'
		qui putexcel C`index' = `tempPiVar'
		qui putexcel D`index' = `tempDelta'
		qui putexcel E`index' = `tempDeltaVar'
		qui putexcel F`index' = `tempCovar'
		qui putexcel G`index' = `tempPrimaryAR'
		qui putexcel H`index' = `tempPrimaryPVal'
		qui putexcel I`index' = `tempAltAR'
		qui putexcel J`index' = `tempAltPVal'
		qui putexcel K`index' = `tempAltAR2'
		qui putexcel L`index' = `tempAltPVal2'
		qui putexcel M`index' = `tempAltAR3'
		qui putexcel N`index' = `tempAltPVal3'
		qui putexcel O`index' = `tempAltAR4'
		qui putexcel P`index' = `tempAltPVal4'
		qui putexcel Q`index' = `tempBeta'
		
		local index = `index'+1
		
		restore
	}	
